﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.ServiceModel.Configuration;
using System.ServiceModel;
using System.Security.Cryptography;
using System.Diagnostics;


namespace YY.Host
{
    class Program
    {

        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);  

        static List<ServiceHost> _hosts = new List<ServiceHost>();

        static void Main(string[] args)
        {
            StartServices();
        }

        public static void StartService()
        {
            try
            {
                using (ServiceHost host = new ServiceHost(typeof(YY.WCFLib.BaseData)))
                {
                    host.Open();
                    Console.WriteLine("服务 [ " + typeof(YY.WCFLib.BaseData).Name + " ] 已经启动。");
                    Console.Read();
                }
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        /// <summary>
        /// 批量启动服务
        /// </summary>
        public static void StartServices()
        {
            try
            {   
                Configuration myConfiguration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
                ServiceModelSectionGroup myServiceModelSectionGroup = (ServiceModelSectionGroup)myConfiguration.GetSectionGroup("system.serviceModel");
                Console.WriteLine("开始启动服务。。。");
                //string AssemblyStr = ", Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

                foreach (ServiceElement element in myServiceModelSectionGroup.Services.Services)
                {
                    //Type mytype = Type.GetType(element.Name + ","+ element.Name.Split('.')[0] + AssemblyStr, true, true);
                    Type mytype = Assembly.LoadFrom("YY.WCFLib.dll").GetType(element.Name, true, true);
                    if (mytype == null)
                    {
                        throw new Exception("没发现服务：" + element.Name);
                    }

                    ServiceHost host = new ServiceHost(mytype);
                    host.Open();
                    _hosts.Add(host);
                    Console.WriteLine(element.Name + " 服务已经启动");                    
                    
                }

                Console.ReadLine();

            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        /// <summary>
        /// 批量关闭服务
        /// </summary>
        public static void CloseServices()
        {
            foreach (ServiceHost host in _hosts)
            {
                host.Close();
            }
        }

        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMD5Hash(String input)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(input), 0, input.Length);
            //char[] temp = new char[res.Length];
            //System.Array.Copy(res, temp, res.Length);
            //return new String(temp);
            return Convert.ToBase64String(res); //返回长度为25字节字符串

        }
    }
}
